package cmd

import (
	"fmt"
	"time"

	"github.com/spf13/cobra"
	"downloader/internal/ab"
)

var (
	concurrency int
	totalRequests int
	timeout int
)

var abCmd = &cobra.Command{
	Use:   "ab [url]",
	Short: "HTTP压力测试工具",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		config := ab.Config{
			Concurrency:   concurrency,
			TotalRequests: totalRequests,
			Timeout:       time.Duration(timeout) * time.Millisecond,
		}
		
		result, err := ab.RunTest(args[0], config)
		if err != nil {
			fmt.Printf("压力测试失败: %v\n", err)
			return
		}

		fmt.Println(result.Report())
	},
}

func init() {
	abCmd.Flags().IntVarP(&concurrency, "concurrency", "c", 1, "并发数")
	abCmd.Flags().IntVarP(&totalRequests, "requests", "n", 10, "总请求数")
	abCmd.Flags().IntVarP(&timeout, "timeout", "t", 5000, "超时时间(毫秒)")
	rootCmd.AddCommand(abCmd)
}